package com.aso114.project.exam;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatRadioButton;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.AnswerCardAdapter;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.bean.EvenBaseBean;
import com.aso114.project.bean.MsgEvent;
import com.aso114.project.bean.QuestionBaseBean;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.util.Constant;
import com.aso114.project.util.MySharedPreferences;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by asus on 2019/1/8.
 */

public class QuestionFragment extends BaseFragment {
    @BindView(R.id.que_img)
    ImageView queImg;
    @BindView(R.id.que_title)
    TextView queTitle;
    @BindView(R.id.que_min_title)
    TextView queMinTitle;
    @BindView(R.id.que_group)
    RadioGroup queGroup;
    @BindView(R.id.que_check_layout)
    LinearLayout queCheckLayout;
    @BindView(R.id.que_see_analysis)
    TextView queSeeAnalysis;
    @BindView(R.id.que_answer)
    TextView queAnswer;
    @BindView(R.id.que_statistics)
    TextView queStatistics;
    @BindView(R.id.que_analysis)
    TextView queAnalysis;
    @BindView(R.id.que_analysis_ly)
    LinearLayout queAnalysisLy;
    @BindView(R.id.que_analysis_all_ly)
    LinearLayout queAnalysisAllLy;
    Unbinder unbinder;

    List<AppCompatRadioButton> listRadio;
    List<AppCompatCheckBox> listCheck;
    AppCompatCheckBox checkBox;
    AppCompatRadioButton appCompatRadioButton;
    ArrayList<QuestionBaseBean> result;

    private int position;
    private int answer;
    private boolean isAnalysis;
    private boolean isAnalysisOpen;
    private int size = 3;
    private float minSize = 11;
    private float maxSize = 15;
    String date1, date2 = "";

    public static QuestionsFragment newInstance(int position, boolean isAnalysis, boolean isAnalysisOpen) {
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putBoolean("isAnalysis", isAnalysis);
        args.putBoolean("isAnalysisOpen", isAnalysisOpen);
        QuestionsFragment fragment = new QuestionsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_questions;
    }

    @Override
    protected void initView() {
        position = getArguments().getInt("position");
        isAnalysis = getArguments().getBoolean("isAnalysis", false);
        isAnalysisOpen = getArguments().getBoolean("isAnalysisOpen", false);
        listRadio = new ArrayList<>();
        listCheck = new ArrayList<>();
        struct();
        if (isAnalysis) {
            queAnalysisAllLy.setVisibility(View.VISIBLE);
            queAnalysisLy.setVisibility(View.GONE);
        } else {
            queAnalysisAllLy.setVisibility(View.GONE);
        }
        if (isAnalysisOpen) {
            queSeeAnalysis.performClick();
        }
        size = MySharedPreferences.getInstance(getActivity()).getTextSize();
        setTitlesize(size);
        inData();
        initListner();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            //显示 开始计时

            // 获取当前时间
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date1 = formatter.format(currentTime);

            System.out.println("本题当前时间====" + date1);

        }

    }

    @Override
    public void onResume() {
        super.onResume();
        size = MySharedPreferences.getInstance(getActivity()).getTextSize();
        setTitlesize(size);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setClick(MsgEvent msgEvent) {
        switch (msgEvent.getItemClick()) {
            case 1:// 小号  13  9
                minSize = 9;
                maxSize = 13;
                break;
            case 2:// 中号  14  10
                minSize = 10;
                maxSize = 14;
                break;
            case 3:// 标准  15  11
                minSize = 11;
                maxSize = 15;
                break;
            case 4:// 大号  16  12
                minSize = 12;
                maxSize = 16;
                break;
            case 5:// 巨无霸 17  13
                minSize = 13;
                maxSize = 17;
                break;
        }
        if (msgEvent.getPageCount() == position) {
            queMinTitle.setTextSize(minSize);

            queTitle.setTextSize(maxSize);
            queAnswer.setTextSize(maxSize);
            queStatistics.setTextSize(maxSize);
            queAnalysis.setTextSize(maxSize);

            if (checkBox != null) {
                checkBox.setTextSize(maxSize);

            }
            if (appCompatRadioButton != null) {
                appCompatRadioButton.setTextSize(maxSize);
            }
        }
    }

    private void setTitlesize(int size) {
        switch (size) {
            case 1:// 小号  13  9
                minSize = 9;
                maxSize = 13;
                break;
            case 2:// 中号  14  10
                minSize = 10;
                maxSize = 14;
                break;
            case 3:// 标准  15  11
                minSize = 11;
                maxSize = 15;
                break;
            case 4:// 大号  16  12
                minSize = 12;
                maxSize = 16;
                break;
            case 5:// 巨无霸 17  13
                minSize = 13;
                maxSize = 17;
                break;
        }
        queMinTitle.setTextSize(minSize);

        queTitle.setTextSize(maxSize);
        queAnswer.setTextSize(maxSize);
        queStatistics.setTextSize(maxSize);
        queAnalysis.setTextSize(maxSize);

        if (checkBox != null) {
            checkBox.setTextSize(maxSize);

        }
        if (appCompatRadioButton != null) {
            appCompatRadioButton.setTextSize(maxSize);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(EvenBaseBean event) {
        Log.i("yxp", event.hashCode() + "----");
        if (event.getType() == 1)
            result = event.getBaseBean();
    }

    private void inData() {
        //获取答案在第几个
        queTitle.setText(Html.fromHtml(result.get(position).getFormatContent(), imgGetter, null));
        queMinTitle.setText("(" + result.get(position).getRealPaperTitle() + ")");
        if (result.get(position).getQuestionsAnswerInfo().getAnswerArray().size() != 0) {
            queAnswer.setText(Html.fromHtml("正确答案" + "<font color=\'#00bf62\'>" + result.get(position).getQuestionsAnswerInfo().getAnswerArray().get(0)));
        }
        queStatistics.setText(Html.fromHtml("个人统计：作答本题<font color=\'#00bf62\'>" + result.get(position).getAnswerCount() + "</font><font color=\'#333333\'>次，做错</font><font color=\'#ff6559\'>" + result.get(position).getAnswerErrorCount() + "</font>次"));
        queAnalysis.setText(Html.fromHtml(result.get(position).getQuestionsAnswerInfo().getFormatContent(), imgGetter, null));

        if (result.get(position).getQuestionsAnswerInfo().getAnswerArray().toString().equals("[A]")) {
            answer = 1;
            System.out.println("正确答案====A");
        }
        if (result.get(position).getQuestionsAnswerInfo().getAnswerArray().toString().equals("[B]")) {
            answer = 2;
            System.out.println("正确答案====B");
        }
        if (result.get(position).getQuestionsAnswerInfo().getAnswerArray().toString().equals("[C]")) {
            answer = 3;
            System.out.println("正确答案====C");
        }
        if (result.get(position).getQuestionsAnswerInfo().getAnswerArray().toString().equals("[D]")) {
            answer = 4;
            System.out.println("正确答案====D");
        }

        if (answer <= 4) {
            //单选
            initRadioButton();
        } else {
            //多选
            initCheckBox();
        }

        //显示图片
        if (result.get(position).getFormatImages().size() == 0)
            queImg.setVisibility(View.GONE);

        else {
            String url = "";
            for (int i = 0; i < result.get(position).getFormatImages().size(); i++) {
                url = result.get(position).getFormatImages().get(i).toString();


            }
            DisplayMetrics dm = new DisplayMetrics();
            int width = dm.widthPixels;
            Picasso.with(getContext()).load(url).placeholder(R.drawable.loading).into(queImg);
        }

    }

    private void initRadioButton() {
        for (int i = 0; i < result.get(position).getQuestioncontentList().size(); i++) {
            if (!TextUtils.isEmpty(result.get(position).getQuestioncontentList().get(i).getSelectValue())) {
                addRadioButtonView(result.get(position).getQuestioncontentList().get(i).getSelectKey() + " " + result.get(position).getQuestioncontentList().get(i).getSelectValue());
            }
        }

        if (result.get(position).isFinishAnswer()) {//判断当前题目是否已经答题答过了，如果是的话，就恢复答题数据，并且设置不可点击

            radioButtonClickEnable();

            if (!result.get(position).isChooseResult()) {//如果没有选择到正确答案的话，就要显示错误答案，否则不显示
                AppCompatRadioButton radio = listRadio.get(result.get(position).getErrorAnswer() - 1);
                radio.setTextColor(getContext().getResources().getColor(R.color.aff6559));
                setErrorDrable(radio);
            }
            AppCompatRadioButton radio = listRadio.get(result.get(position).getRightAnswer() - 1);
            radio.setTextColor(getContext().getResources().getColor(R.color.a00BF62));
            setRightDrable(radio);

        }
    }

    //多选题
    private void initCheckBox() {

        for (int i = 0; i < result.get(position).getQuestioncontentList().size(); i++) {
            if (!TextUtils.isEmpty(result.get(position).getQuestioncontentList().get(i).getSelectValue())) {
                addCheckBoxView(result.get(position).getQuestioncontentList().get(i).getSelectKey() + " " + result.get(position).getQuestioncontentList().get(i).getSelectValue());
            }
        }

        if (result.get(position).isFinishAnswer()) {//已答完题目
            checkBoxClickEnable();//多选不可点击
            //遍历用户的选择
            for (int i = 0; i < result.get(position).getResultList().size(); i++) {
                int a = result.get(position).getResultList().get(i);//拿到答题的标号
                listCheck.get(a - 1).setTextColor(getContext().getResources().getColor(R.color.aff6559));
                listCheck.get(a - 1).setChecked(true);
            }

            for (int i = 0; i < result.get(position).getRightList().size(); i++) {
                int a = result.get(position).getRightList().get(i);//拿到答题的标号
                listCheck.get(a - 1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
            }

        } else {
            //添加一个确定按钮
            AppCompatButton appCompatButton = new AppCompatButton(getContext());
            appCompatButton.setText("确定");
            appCompatButton.setBackgroundColor(getContext().getResources().getColor(R.color.colorPrimary));
            appCompatButton.setTextColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(20, 30, 20, 20);
            appCompatButton.setLayoutParams(params);
            queCheckLayout.addView(appCompatButton);
            appCompatButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    doHandle();
                }
            });
        }
    }

    Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
            URL url;
            try {
                url = new URL(source);
                drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth() + 50,
                    drawable.getIntrinsicHeight() + 50);
            return drawable;
        }
    };

    public static void struct() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
                .penaltyLog() // 打印logcat
                .penaltyDeath().build());
    }

    public void addCheckBoxView(String question) {
        checkBox = new AppCompatCheckBox(getContext());
        checkBox.setGravity(Gravity.START);
        checkBox.setText(Html.fromHtml(question, imgGetter, null));
        checkBox.setTextSize(maxSize);
        checkBox.setTextColor(getContext().getResources().getColor(R.color.black333));
        RadioGroup.LayoutParams param = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        param.setMargins(10, 10, 10, 10);
        checkBox.setLayoutParams(param);
        queCheckLayout.addView(checkBox);
        listCheck.add(checkBox);
    }


    public void addRadioButtonView(String question) {
        appCompatRadioButton = new AppCompatRadioButton(getContext());
        appCompatRadioButton.setGravity(Gravity.START);
        appCompatRadioButton.setText(Html.fromHtml(question, imgGetter, null));
        Drawable bitmapDrawable = (Drawable) getResources().getDrawable(R.drawable.ic_default);
        appCompatRadioButton.setButtonDrawable(bitmapDrawable);
        appCompatRadioButton.setTextSize(maxSize);
        appCompatRadioButton.setTextColor(getContext().getResources().getColor(R.color.black333));
        RadioGroup.LayoutParams param = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        param.setMargins(10, 10, 10, 10);
        appCompatRadioButton.setLayoutParams(param);
        queGroup.addView(appCompatRadioButton);
        listRadio.add(appCompatRadioButton);
    }

    public void setErrorDrable(AppCompatRadioButton appCompatRadioButton) {
        Drawable bitmapDrawable = (Drawable) getResources().getDrawable(R.mipmap.home_cuo_ic);
        appCompatRadioButton.setButtonDrawable(bitmapDrawable);
    }


    public void setRightDrable(AppCompatRadioButton appCompatRadioButton) {
        Drawable bitmapDrawable = (Drawable) getResources().getDrawable(R.mipmap.home_dui_ic);
        appCompatRadioButton.setButtonDrawable(bitmapDrawable);
    }

    void radioButtonClickEnable() {
        for (AppCompatRadioButton radioButton :
                listRadio) {
            radioButton.setClickable(false);
        }
    }

    void checkBoxClickEnable() {
        for (AppCompatCheckBox checkbos :
                listCheck) {
            checkbos.setClickable(false);
        }
    }

    //处理点击事件
    private void doHandle() {
        switch (answer) {
            case 7://AB 也就是1、2选项为正确
                //存储正确答案
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                break;
            case 8://AC 也就是1、3
                //存储正确答案
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 9://AD 也就是1、4
                //存储正确答案
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                break;
            case 10://BC 也就是2、3
                //存储正确答案
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 11://BD 也就是2、4
                //存储正确答案
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                break;
            case 12://CD 也就是3、4
                //存储正确答案
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));


                break;
            case 13://ABC 也就是1、2、3

                //存储正确答案
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 14://ABD 也就是1、2、4
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 15://ACD 也就是1、3、4
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 16://BCD 也就是2、3、4
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));

                break;
            case 17://ABCD 也就是1、2、3、4
                result.get(position).getRightList().add(1);
                listCheck.get(0).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(2);
                listCheck.get(1).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(3);
                listCheck.get(2).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                result.get(position).getRightList().add(4);
                listCheck.get(3).setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                break;
        }
        //然后进行筛选
        commonJudge();

    }

    void commonJudge() {
        //存储用户选择的答案
        for (int i = 0; i < listCheck.size(); i++) {
            if (listCheck.get(i).isChecked()) {  //遍历查询当前是否选中
                result.get(position).getResultList().add(i + 1);
                System.out.println("本体选择的答案是===" + result.get(position).getResultList().get(i).toString());
            }
        }


        //先判断用户输入的和答案的选择个数是否相同
        if (result.get(position).getResultList().size() == result.get(position).getRightList().size()) {
            for (int i = 0; i < result.get(position).getResultList().size(); i++) {
                if (result.get(position).getResultList().get(i) != result.get(position).getRightList().get(i)) {
                    result.get(position).setChooseResult(false);//遍历如果有答案不相同时，则选择答题失败，并跳出
                    //如果有错误的话就把错误答案显示出来
                    for (int j = 0; j < listCheck.size(); j++) {
                        if (listCheck.get(j).isChecked()) {  //遍历查询当前是否选中
                            listCheck.get(j).setTextColor(getContext().getResources().getColor(R.color.aff6559));
                        }
                    }
                    break;
                } else {
                    result.get(position).setChooseResult(true);//这一步骤可以设置为true，因为只要有不同就直接跳出了，并且设置了false，如果不走的话，就是为true
                }
            }
            if (result.get(position).isChooseResult())

                if (Constant.title_tv.equals("3") || Constant.title_tv.equals("4")) {
//                    titletv.setText("真题练习");titletv.setText("巩固练习");
                    QuestionTestActivity.nextQuestion();
                }

        } else {


            if (Constant.title_tv.equals("3") || Constant.title_tv.equals("4")) {
//                    titletv.setText("真题练习");titletv.setText("巩固练习");
                QuestionTestActivity.nextQuestion();
            } else {
                //如果有错误的话就把错误答案显示出来
                for (int j = 0; j < listCheck.size(); j++) {
                    if (listCheck.get(j).isChecked()) {  //遍历查询当前是否选中
                        listCheck.get(j).setTextColor(getContext().getResources().getColor(R.color.aff6559));
                    }
                }

            }
            //记录错误
            result.get(position).setChooseResult(false);
        }
        result.get(position).setFinishAnswer(true);
        checkBoxClickEnable();//多选不可点击
        QuestionTestActivity.upDataRightAndError();//更新MainActivity
    }

    private void initListner() {
        queGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup rd, int id) {

                if (Constant.title_tv.equals("7") || Constant.title_tv.equals("8")) {
                    //错题练习 与收藏 不可点击
                    for (int i = 0; i < listRadio.size(); i++) {
                        AppCompatRadioButton radioButton = listRadio.get(i);

                    }
                    radioButtonClickEnable();//设置不可点击
                } else {

                    for (int i = 0; i < listRadio.size(); i++) {
                        AppCompatRadioButton radioButton = listRadio.get(i);
                        //遍历查找找到当前点击的item
                        if (radioButton.getId() == id) {

                            if ((i + 1) == answer) {//判断选择是否是正确答案
                                System.out.println("用户选择的答案是正确答案===" + (i + 1));

                                if (Constant.title_tv.equals("3") || Constant.title_tv.equals("4")) {
                                    radioButton.setTextColor(getContext().getResources().getColor(R.color.aff6559));
                                } else {
                                    radioButton.setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                                }

                                result.get(position).setRightAnswer(i + 1);//设置用户答对的是什么答案
                                result.get(position).setChooseResult(true);//存储用户选择的答案为正确的
                                setRightDrable(radioButton);//设置样式
                                result.get(position).setQuestiontime((int) gettime());//获取每道题答题时间
                                QuestionTestActivity.nextQuestion();
                            } else {//选择的是错误答案
                                System.out.println("用户选择的答案是错误答案===" + (i + 1));

                                if (Constant.title_tv.equals("3") || Constant.title_tv.equals("4")) {
                                    radioButton.setTextColor(getContext().getResources().getColor(R.color.aff6559));
                                    setRightDrable(radioButton);//设置样式

                                    result.get(position).setErrorAnswer(i + 1);//设置选错题目的标识 用户答错的是什么答案
                                    result.get(position).setChooseResult(false);//存储用户选择的答案为错误的
                                    setErrorDrable(radioButton);

                                    result.get(position).setQuestiontime((int) gettime());//获取每道题答题时间
                                    QuestionTestActivity.nextQuestion();

                                } else {
                                    //其余的可以展开查看解释
                                    try {
                                        result.get(position).setQuestiontime((int) gettime());//获取每道题答题时间
                                        radioButton.setTextColor(getContext().getResources().getColor(R.color.aff6559));
                                        setErrorDrable(radioButton);//设置样式

                                        listRadio.get(answer - 1).setTextColor(getContext().getResources().getColor(R.color.right));
                                        setRightDrable(listRadio.get(answer - 1));

                                        result.get(position).setErrorAnswer(i + 1);//设置选错题目的标识 用户答错的是什么答案
                                        result.get(position).setChooseResult(false);//存储用户选择的答案为错误的
                                        setErrorDrable(radioButton);

                                        queSeeAnalysis.setVisibility(View.GONE);
                                        queAnalysisLy.setVisibility(View.VISIBLE);
                                    } catch (ArrayIndexOutOfBoundsException e) {
                                        System.out.println("数组越界！");
                                        e.printStackTrace();
                                    }

                                }

                            }
                            result.get(position).setRightAnswer(answer);//设置选对题目的标识 用户答对的是什么答案
                            result.get(position).setFinishAnswer(true);//设置完成了答题
                            radioButtonClickEnable();//设置不可点击

                            QuestionTestActivity.upDataRightAndError();//更新MainActivity
                            break;
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addOnClick() {

    }

    public long gettime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date2 = formatter.format(currentTime);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("答完答题时间====" + date2);
        long second = 0;
        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            long diff = d2.getTime() - d1.getTime();// 这样得到的差值是微秒级别


            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24))
                    / (1000 * 60 * 60);
            second = (diff - days * (1000 * 60 * 60 * 24) - hours
                    * (1000 * 60 * 60))
                    / (1000);


            System.out.println("本题答题时间====" + second + "秒");

        } catch (Exception e) {
        }

        return second;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        EventBus.getDefault().register(this);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.que_see_analysis)
    public void onViewClicked() {
        queSeeAnalysis.setVisibility(View.GONE);
        queAnalysisLy.setVisibility(View.VISIBLE);
        if (Constant.title_tv.equals("7") || Constant.title_tv.equals("8")) {
            for (int i = 0; i < listRadio.size(); i++) {
                AppCompatRadioButton radioButton = listRadio.get(i);
                //遍历查找找到当前点击的item
                if ((i + 1) == answer) {//判断选择是否是正确答案
                    System.out.println("用户选择的答案是正确答案===" + (i + 1));
                    radioButton.setTextColor(getContext().getResources().getColor(R.color.a00BF62));
                    setRightDrable(radioButton);//设置样式
                }

                radioButtonClickEnable();//设置不可点击

            }


        }
    }

}
